#!/usr/bin/env/ python
# -*- coding: utf-8 -*-

import os,json

def find_locs(line):
    # xMin, yMin, xMax, yMax, score, classID,_ = line.rstrip().split(",")
    xMin, yMin, xMax, yMax,  classID, _ ,score= line.rstrip().split(",")
    xMin, yMin, xMax, yMax, score, classID = [i.split("=")[-1] for i in[xMin, yMin, xMax, yMax, score, classID]]
    xMin, yMin, xMax, yMax = map(int, map(float, [xMin, yMin, xMax, yMax]))

    for items in [i.replace(" ","") for i in line.rstrip().split(",")]:
        loc = items.split("=")
        if ("xMin" == loc[0]):
            xMin = int(float(loc[1]))
        if ("yMin" == loc[0]):
            yMin = int(float(loc[1]))
        if ("xMax" == loc[0]):
            xMax = int(float(loc[1]))
        if ("yMax" == loc[0]):
            yMax = int(float(loc[1]))
        if ("score" == loc[0]):
            try:
                score = int(100*float(loc[1].replace("%", "")))
            except:
                score = int(float(loc[1]))

        if ("classID" == loc[0]):
            classID = int(float(loc[1]))


    x, y, w, h = xMin, yMin, xMax - xMin, yMax - yMin
    print("x,y,w,h:", x, y, w, h)
    return x,y,w,h,score,classID

def explainWithJson(src):
    with open(src,'r') as f_src:
        locs, pathName = [], ""
        lines = f_src.readlines()
        for line in lines:
            print(line)
        loc = ""
        locMsg = []
        for loc in locs:
            if type(loc) is not list:
            # if(loc.startswith("/")):
                pathName = loc
            else:
                x, y, w, h, score, classID = loc
                locMsg.append(loc)
            msg = {pathName:locMsg}

        print(msg)

        for l in loc:
            if(len(l)==2):
                l1,l2 = loc
                if(len(l1)>0 and len(l2)>0):
                    x, y, w, h, score, classID = l1
                    pathName = l2[0]
                    msg = {"x":x,"y":y,"w":w,"h":h,"score":score,"classID":classID,"pathName":pathName}
                    all.append(msg)

        # with open("detectResult.txt","w") as f:
        #     js = json.dumps(all)
        #     f.write(js)


def explain(src,dst):
    with open(src,'r') as f_src:
        locs, pathName = [], ""
        lines = f_src.readlines()
        for line in lines:
            print(line)
            if (line.startswith("pathName")):
                print(line)
                pathName = line.split(":")[-1]
                name = pathName.split("/")[-1].rstrip("\n").split(".")[0]
                f = open(dst + name + ".txt", "a+")


            if(line.startswith("xMin")):
                x,y,w,h,score,classID = find_locs(line)
                # xMin, yMin, xMax, yMax,  classID, _ ,score = find_locs(line)

                locs.append([x,y,w,h,score,classID])
                f.write(",".join(map(str, [x,y,w,h,score,classID]))+"\n")

            if ("time---" in line):
                useTime = line.split(":")[-1]
                f.write("useTime:"+useTime)


def main():
    # src = "/mnt/data/3519/demo/mpp/sample/boat_building_vehicle/nnie/history.txt"
    src = "/mnt/data/3519_/demo/mpp/sample/3559/sample_src/mpp/sample/svp/head/ins_nnie/log.txt"
    dst = "../headSrc/"
    explain(src,dst)

if __name__ == "__main__":
    main()